// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:'',
    essaylist:[],
    interestor:false
  },
details(){
  wx.navigateTo({
    url: '/pages/details/details',
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userId;
    console.log(options.findid)
    wx.request({
      url: 'http://localhost:3000/user/other',
      method: 'GET',
      data:{
        id:options.findid
      },
      success:res=>{
        this.setData({user:res.data})
        console.log(this.data)
        console.log(this.data.user.birthady)
        let date=new Date(this.data.user.birthady)
        let newdate=date.getFullYear()+'-'+Number(date.getMonth()+1)+'-'+date.getDate()
        console .log(newdate)
        this.setData({'user.birthady':newdate})
        userId=res.data.id
      },
      fail:err=>{
        console.log(err)
      }
    })
    wx.request({
      url: 'http://localhost:3000/essay/byuser',
      method: 'GET',
      data:{
        userid:options.findid
      },
      success:res=>{
        this.setData({essaylist:res.data})
        console.log(this.data)
      },
      fail:err=>{
        console.log(err)
      }
    })
    wx.request({
      url: 'http://localhost:3000/user/check',
      data:{
        id:getApp().globalData.user.id,
        otherid:options.findid,
      },
      success:res=>{
        console.log(res.data)
        if(res.data.count===0){
          this.setData({interestor:false})
        }
        else{
          this.setData({interestor:true})
        }
        console.log(res.data.count)
      },
      fail:err=>{
        console.log(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
  formatDate: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }

    var second = function () { //获取秒数
        return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    }
    return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ':' + second()

},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  details(e){
  let id=e.currentTarget.id
  wx.navigateTo({
    url: '/pages/details/details?id='+id,
  })
},
  goattention(){
    wx.navigateTo({
      url: '/pages/interestother/interestother?id='+this.data.user.id,
    })
  },
  gofans(){
    wx.navigateTo({
      url: '/pages/fanother/fanother?id='+this.data.user.id,
    })
  },
  add(){
    let date=new Date();
    date=this.formatDate(date)
    console.log(date)
    wx.request({
      url: 'http://localhost:3000/user/add',
      data:{
        id:getApp().globalData.user.id,
        otherid:this.data.user.id,
        fanTime:date
      },
      success:res=>{
        console.log(res)
        this.setData({interestor:true})
        getApp().globalData.user.attention=getApp().globalData.user.attention+1
      },
      fail:err=>{
        console.log(err)
      }
    })
  },
  del(){
    wx.request({
      url: 'http://localhost:3000/user/del',
      data:{
        id:getApp().globalData.user.id,
        otherid:this.data.user.id,
      },
      success:res=>{
        this.setData({interestor:false})
        getApp().globalData.user.attention=getApp().globalData.user.attention-1
        console.log(res)
      },
      fail:err=>{
        console.log(err)
      }
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  check(){

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})